const db = require("../db/index");

const depamentMsgController = {
  // 获取部门消息id列表
  getDepartmentMsg: async (req, res) => {
    const { id, department } = req.query;
    const sql = "SELECT * FROM message WHERE message_receipt_object = ?";
    try {
      const [msg] = await db.query(sql, department);
      let msgArr = [];
      msg.forEach((item) => {
        msgArr.push(item.id);
      });
      const sql1 =
        "UPDATE users SET read_list = ?, read_status = 1 WHERE id = ?";
      await db.query(sql1, [JSON.stringify(msgArr), id]);
      res.send({
        status: 1,
        message: "成功",
        data: msgArr,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取部门消息
  getDepartmentMsgList: async (req, res) => {
    const { department } = req.query;
    const sql =
      "SELECT * FROM message WHERE message_status = '1' AND message_receipt_object = ?";
    try {
      const [msg] = await db.query(sql, department);
      res.send({
        status: 1,
        message: "成功",
        data: msg,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 返回用户的阅读列表及状态
  getReadListAndStatus: async (req, res) => {
    const { id } = req.query;
    const sql = "SELECT read_list,read_status FROM users WHERE id = ?";
    try {
      const [data] = await db.query(sql, id);
      res.send({
        status: 1,
        message: "成功",
        data: data,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 用户点击消息后对 read_list 删减
  updateReadList: async (req, res) => {
    const { id, readId } = req.body;
    const sql = "SELECT read_list FROM users WHERE id = ?";
    try {
      const [data] = await db.query(sql, id);
      let list = JSON.stringify(
        JSON.parse(data[0].read_list).filter((item) => {
          return item != readId;
        })
      );

      const sql1 = "UPDATE users SET read_list = ? WHERE id = ?";
      await db.query(sql1, [list, id]);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 把新发布文章的id插入到当前所属部门的rad_list当中
  changUserReadList: async (req, res) => {
    const { messageId, department } = req.body;
    const sql =
      "SELECT read_list,read_status,id FROM users WHERE department = ?";
    try {
      const [data] = await db.query(sql, department);
      data.forEach(async (item) => {
        if (item.read_status === 1) {
          let arr = JSON.parse(item.read_list);
          arr.push(JSON.parse(messageId));
          arr = JSON.stringify(arr);
          const sql1 = "UPDATE users SET read_list = ? WHERE id = ?";
          await db.query(sql1, [arr, item.id]);
        }
      });

      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 删除的文章id从readlist移除
  DeleteUserReadListId: async (req, res) => {
    const { deleteId, department } = req.body;
    const sql =
      "SELECT read_list,read_status,id FROM users WHERE department = ?";
    try {
      const [data] = await db.query(sql, department);
      data.forEach(async (item) => {
        if (item.read_status === 1) {
          let arr = JSON.parse(item.read_list);
          arr = arr.filter((item) => {
            return item != deleteId;
          });
          arr = JSON.stringify(arr);
          const sql1 = "UPDATE users SET read_list = ? WHERE id = ?";
          await db.query(sql1, [arr, item.id]);
        }
      });

      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
};

module.exports = depamentMsgController;
